package com.zhonghui.modlules.product.eshopService.Impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.eshop.domain.BaseDomain;
import com.eshop.service.Impl.BaseServiceImpl;
import com.zhonghui.common.eshopDozer.config.IGenerator;
import com.zhonghui.common.eshopEnum.*;
import com.zhonghui.common.eshopVo.StoreProductQueryVo;
import com.zhonghui.modlules.product.eshopDomain.StoreProduct;
import com.zhonghui.modlules.product.eshopMapper.StoreProductMapper;
import com.zhonghui.modlules.product.eshopParam.StoreProductQueryParam;
import com.zhonghui.modlules.product.eshopService.StoreProductAttrValueService;
import com.zhonghui.modlules.product.eshopService.StoreProductService;
import com.zhonghui.modlules.product.eshopVo.StoreProductAttrValue;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import com.qiniu.util.StringUtils;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional(
        propagation = Propagation.REQUIRED,
        rollbackFor = {Exception.class}
)
public class StoreProductServiceImpl extends BaseServiceImpl<StoreProductMapper, StoreProduct> implements StoreProductService {
    @Autowired
    private  StoreProductMapper storeProductMapper;
    @Autowired
    private IGenerator generator;
    @Autowired
    private StoreProductAttrValueService storeProductAttrValueService;
    @Override
    public List<StoreProductQueryVo> getList(int page, int limit, int order) {
        LambdaQueryWrapper<StoreProduct> wrapper = new LambdaQueryWrapper();
        wrapper.eq( StoreProduct::getIsShow, ShopCommonEnum.SHOW_1.getValue())
                .eq( BaseDomain::getIsDel,ShopCommonEnum.DEFAULT_0.getValue() )
                .orderByDesc( StoreProduct::getSort );
        wrapper.eq( StoreProduct::getIsIntegral,0 );
        wrapper.eq(StoreProduct::getIsIntegral, 0);
        switch(ProductEnum.toType(order)) {
            case TYPE_1:
                wrapper.eq(StoreProduct::getIsBest, ShopCommonEnum.IS_STATUS_1.getValue());
                break;
            case TYPE_3:
                wrapper.eq(StoreProduct::getIsNew, ShopCommonEnum.IS_STATUS_1.getValue());
                break;
            case TYPE_4:
                wrapper.eq(StoreProduct::getIsBenefit, ShopCommonEnum.IS_STATUS_1.getValue());
                break;
            case TYPE_2:
                wrapper.eq(StoreProduct::getIsHot, ShopCommonEnum.IS_STATUS_1.getValue());
        }

        Page<StoreProduct> pageModel = new Page((long)page, (long)limit);
        IPage<StoreProduct> pageList = this.storeProductMapper.selectPage(pageModel, wrapper);
        return this.generator.convert(pageList.getRecords(), StoreProductQueryVo.class);
    }

    public Map<String, Object> getGoodsListByPage(StoreProductQueryParam productQueryParam) {
        LambdaQueryWrapper<StoreProduct> wrapper = new LambdaQueryWrapper();
        wrapper.eq( StoreProduct::getIsShow, CommonEnum.SHOW_STATUS_1.getValue() );
        wrapper.eq( BaseDomain::getIsDel,CommonEnum.DEL_STATUS_0.getValue() );
        if (productQueryParam.getIsIntegral() != null){
            wrapper.eq( StoreProduct::getIsIntegral,productQueryParam.getIsIntegral() );
        }
        if (StrUtil.isNotBlank(productQueryParam.getSid()) && !"0".equals( productQueryParam.getSid() )){
            wrapper.eq( StoreProduct::getCateId,productQueryParam.getSid() );
        }

        if (StrUtil.isNotEmpty(productQueryParam.getKeyword())) {
            wrapper.and((wrapper1) -> {
                wrapper1.or();
                wrapper1.like(StoreProduct::getStoreName, productQueryParam.getKeyword());
                wrapper1.or();
                wrapper1.like(StoreProduct::getStoreInfo, productQueryParam.getKeyword());
                wrapper1.or();
                wrapper1.like(StoreProduct::getKeyword, productQueryParam.getKeyword());
            });
        }

        if (StrUtil.isNotBlank(productQueryParam.getNews()) && !"0".equals(productQueryParam.getNews())) {
            wrapper.eq(StoreProduct::getIsNew, ShopCommonEnum.IS_NEW_1.getValue());
        }

        if (SortEnum.DESC.getValue().equals(productQueryParam.getSalesOrder())) {
            wrapper.orderByDesc(StoreProduct::getSales);
        } else if (SortEnum.ASC.getValue().equals(productQueryParam.getSalesOrder())) {
            wrapper.orderByAsc(StoreProduct::getSales);
        }

        if (SortEnum.DESC.getValue().equals(productQueryParam.getPriceOrder())) {
            wrapper.orderByDesc(StoreProduct::getPrice);
        } else if (SortEnum.ASC.getValue().equals(productQueryParam.getPriceOrder())) {
            wrapper.orderByAsc(StoreProduct::getPrice);
        }

        wrapper.orderByDesc(StoreProduct::getSort);
        if (StringUtils.isNullOrEmpty(productQueryParam.getPriceOrder()) && StringUtils.isNullOrEmpty(productQueryParam.getSalesOrder())) {
            wrapper.orderByDesc(StoreProduct::getId);
        }

        Page<StoreProduct> pageModel = new Page((long)productQueryParam.getPage(), (long)productQueryParam.getLimit());
        IPage<StoreProduct> pageList = this.storeProductMapper.selectPage(pageModel, wrapper);
        List<StoreProductQueryVo> list = this.generator.convert(pageList.getRecords(), StoreProductQueryVo.class);
        Map<String, Object> map = new LinkedHashMap(2);
        map.put("content", list);
        map.put("totalElements", pageList.getTotal());
        return map;
    }

    public int getProductStock(Long productId, String unique, String type) {
        LambdaQueryWrapper<StoreProductAttrValue> qq = new LambdaQueryWrapper<>();
        qq.eq(StoreProductAttrValue::getUnique, unique).eq(StoreProductAttrValue::getProductId, productId);
        StoreProductAttrValue storeProductAttrValue = (StoreProductAttrValue)this.storeProductAttrValueService.getOne(qq);

        if (storeProductAttrValue == null) {
            return 0;
        } else if (ProductTypeEnum.PINK.getValue().equals(type)) {
            return storeProductAttrValue.getPinkStock();
        } else {
            return ProductTypeEnum.SECKILL.getValue().equals(type) ? storeProductAttrValue.getSeckillStock() : storeProductAttrValue.getStock();
        }
    }

    public StoreProductQueryVo getStoreProductById(Long id) {
        return (StoreProductQueryVo)this.generator.convert(((StoreProductMapper)this.baseMapper).selectById(id), StoreProductQueryVo.class);
    }
}
